<template>
  <div>
    <el-dialog :title="info.isadd?'管理员添加':'管理员修改'" :visible.sync="info.isshow" @closed="closed">
      <el-form :model="form">
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="form.roleid" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="form.password" autocomplete="off"></el-input>密 码 留 空 则 不 修 改 ! ! ! ! ! !
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="qx">取 消</el-button>
        <el-button type="primary" v-if="this.info.isadd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="xiugai">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  requseradd,
  reqrolelist,
  requserinfo,
  requseredit,
} from "../../../request/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["info"],
  data() {
    return {
      options: [],
      form: {
        roleid: "",
        username: "",
        password: "",
        status: "2",
      },
    };
  },
  computed: {
    ...mapGetters({
      user: "user",
    }),
  },
  methods: {
    ...mapActions({
      useractions: "useractions",
    }),
    closed() {
      if (!this.info.isadd) {
        this.empty();
      }
    },
    qx() {
      this.info.isshow = false;
    },
    empty() {
      this.form = {
        roleid: "",
        username: "",
        password: "",
        status: "2",
      };
    },
    checkEmpty() {
      return new Promise((resolve, reject) => {
        if (this.form.roleid == "") {
          errorAlert("所属角色不能为空");
          return;
        }
        if (this.form.username == "") {
          errorAlert("用户名不能为空");
          return;
        }
        if (this.form.password == "") {
          errorAlert("密码不能为空");
          return;
        }
        resolve();
      });
    },
    add() {
      this.checkEmpty().then(() => {
        requseradd(this.form).then((res) => {
          if (res.data.code == 200) {
            successAlert(res.data.msg);
            this.info.isshow = false;
            this.empty();
            this.$emit("init");
          }
        });
      });
    },
    one(uid) {
      // console.log(uid);
      requserinfo({ uid: uid }).then((res) => {
        this.form = res.data.list;
        this.form.password = "";
      });
    },
    //修改
    xiugai() {
      this.checkEmpty().then(() => {
        // console.log(uid);
        // console.log(this.form);
        requseredit(this.form).then((res) => {
          if (res.data.code == 200) {
            successAlert(res.data.msg);

            if (this.form.uid === this.user.uid) {
              this.useractions({});
              this.$router.replace("/login");
              return;
            }
            this.info.isshow = false;
            this.empty();
            this.$emit("init");
          }
          // console.log(res);
        });
      });
    },
  },
  mounted() {
    //  value: '选项1',
    //  label: '黄金糕'
    reqrolelist().then((res) => {
      let options = res.data.list;
      this.options = options.map((item) => {
        return {
          value: item.id,
          label: item.rolename,
        };
      });
      // console.log(this.options);
    });
  },
};
</script>

<style>
</style>